﻿//------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2010 , Jirisoft , Ltd. 
//------------------------------------------------------------

using System;
using System.Data;

namespace DotNet.Business
{
    using DotNet.Model;
    using DotNet.Utilities;
    using DotNet.Manager;

    /// <summary>
    /// BaseContactDetailsManager
    /// 内部邮件明细表
    ///
    /// 修改纪录
    ///
    ///     2009-11-04 版本：1.3 Jirigala  增加分类功能，部门公告与邮件有用相同的界面，相同的数据结构。 
    ///     2009-08-27 版本：1.2 Jirigala  已经被删除的用户、已经被设置为无效的用户不应该发邮件。 
    ///     2009-03-24 版本：1.1 Chenbotao 添加发送邮件方法。 
    ///		2009-03-18 版本：1.0 Chenbotao 创建主键。
    ///
    /// 版本：1.1
    ///
    /// <author>
    ///		<name>Chenbotao</name>
    ///		<date>2009-03-19</date>
    /// </author>
    /// </summary>
    /// 
    public partial class BaseContactDetailsManager : BaseManager, IBaseManager
    {
        #region public int GetNewCount(string userId) 获取新信息个数
        /// <summary>
        /// 获取新信息个数
        /// 要记得去掉已删除的邮件
        /// </summary>
        /// <returns>记录个数</returns>
        public int GetNewCount(string userId)
        {
            int returnValue = 0;
            string sqlQuery = " SELECT COUNT(*) "
                            + "   FROM " + base.CurrentTableName
                            + "  WHERE (" + BaseContactDetailsTable.FieldDeleteMark + " = 0 ) "
                            + "        AND (" + BaseContactDetailsTable.FieldIsNew + " = " + ((int)MessageStateCode.New).ToString() + " ) "
                            + "        AND (" + BaseContactDetailsTable.FieldReceiverId + " = '" + userId + "' ) "
                            + "        AND (" + BaseContactDetailsTable.FieldCategory + " = 'User' ) ";
            object returnObject = DbHelper.ExecuteScalar(sqlQuery);
            if (returnObject != null)
            {
                returnValue = int.Parse(returnObject.ToString());
            }
            return returnValue;
        }
        #endregion

        #region public int GetNewCount(string userId, string category) 获取新信息个数
        /// <summary>
        /// 获取新信息个数
        /// 要记得去掉已删除的邮件
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <param name="category">分类类别</param>
        /// <returns>记录个数</returns>
        public int GetNewCount(string userId, string category)
        {
            if (String.IsNullOrEmpty(category))
            {
                return this.GetNewCount(userId);
            }
            int returnValue = 0;

            string sqlQuery = " SELECT COUNT(*) "
                            + "   FROM " + BaseContactTable.TableName
                            + "  WHERE " + BaseContactTable.FieldParentId + "='" + category + "'"
                            + "        AND Id IN ( "
                                                + " SELECT " + BaseContactDetailsTable.FieldContactId
                                                + "   FROM " + base.CurrentTableName
                                                + "  WHERE (" + BaseContactDetailsTable.FieldDeleteMark + " = 0 ) "
                                                + "        AND (" + BaseContactDetailsTable.FieldIsNew + " = " + ((int)MessageStateCode.New).ToString() + " ) "
                                                + "        AND (" + BaseContactDetailsTable.FieldReceiverId + " = '" + userId + "' ) "
                                                + "        AND (" + BaseContactDetailsTable.FieldCategory + " = 'User' ) "
                                                + " ) ";
            object returnObject = DbHelper.ExecuteScalar(sqlQuery);
            if (returnObject != null)
            {
                returnValue = int.Parse(returnObject.ToString());
            }
            return returnValue;
        }
        #endregion

        /// <summary>
        /// 转发给别人看
        /// </summary>
        /// <param name="ContactId">内部联络单主键</param>
        /// <param name="receiverIds">送给</param>
        /// <returns>影响行数</returns>
        public int AddReceivers(string ContactId, string[] receiverIds)
        {
            int returnValue = 0;
            BaseUserManager userManager = new BaseUserManager(DbHelper, UserInfo);
            BaseUserEntity useEntity = null;
            for (int i = 0; i < receiverIds.Length; i++)
            {
                if (!this.Exists(BaseContactDetailsTable.FieldContactId, ContactId, BaseContactDetailsTable.FieldReceiverId, receiverIds[i]))
                {
                    useEntity = userManager.GetEntity(int.Parse(receiverIds[i]));
                    // 是有效的用户，而且是未必删除的用户才发邮件
                    if ((useEntity.Enabled.Equals("1")) && (!useEntity.DeleteMark.Equals("1")))
                    {
                        this.AddReceiver(useEntity, ContactId, receiverIds[i]);
                        returnValue++;
                    }
                }
            }
            // 这里需要重新计算发送给了几个人，几个人已经阅读的功能
            this.SetReadState(ContactId);
            return returnValue;
        }

        public string AddReceiver(string contactId, string receiverId)
        {
            string returnValue = string.Empty;
            // 需要判断是否存在
            string[] names = new string[]{BaseContactDetailsTable.FieldContactId, BaseContactDetailsTable.FieldReceiverId, BaseContactDetailsTable.FieldCategory};
            Object[] values = new Object[] { contactId, receiverId, "User" };
            if (this.Exists(names, values))
            {
                return string.Empty;
            }
            BaseUserManager userManager = new BaseUserManager(DbHelper, UserInfo);
            BaseUserEntity useEntity = userManager.GetEntity(int.Parse(receiverId));
            BaseContactDetailsEntity contactDetailsEntity = new BaseContactDetailsEntity();
            // 这里一定要给个不可猜测的主键，为了提高安全性
            contactDetailsEntity.Id = BaseBusinessLogic.NewGuid();
            contactDetailsEntity.ContactId = contactId;
            contactDetailsEntity.Category = "User";
            contactDetailsEntity.ReceiverId = useEntity.Id.ToString();
            contactDetailsEntity.ReceiverRealName = useEntity.Realname;
            contactDetailsEntity.IsNew = 0;
            contactDetailsEntity.Enabled = 1;
            contactDetailsEntity.NewComment = 0;
            returnValue = this.Add(contactDetailsEntity);
            // 这里需要重新计算发送给了几个人，几个人已经阅读的功能
            this.SetReadState(contactId);
            return returnValue;
        }

        private string AddReceiver(BaseUserEntity useEntity, string ContactId, string receiverId)
        {
            BaseContactDetailsEntity contactDetailsEntity = new BaseContactDetailsEntity();
            // 这里一定要给个不可猜测的主键，为了提高安全性
            contactDetailsEntity.Id = BaseBusinessLogic.NewGuid();
            contactDetailsEntity.ContactId = ContactId;
            contactDetailsEntity.Category = "User";
            contactDetailsEntity.ReceiverId = useEntity.Id.ToString();
            contactDetailsEntity.ReceiverRealName = useEntity.Realname;
            contactDetailsEntity.IsNew = 1;
            contactDetailsEntity.Enabled = 1;
            contactDetailsEntity.NewComment = 0;
            return this.Add(contactDetailsEntity);
        }

        /// <summary>
        /// 移除收件人
        /// </summary>
        /// <param name="ContactId">内部联络单主键</param>
        /// <param name="receiverID">接收者主键</param>
        /// <returns>影响行数</returns>
        public int RemoveReceiver(string ContactId, string receiverId)
        {
            int returnValue = 0;
            returnValue = this.Delete(BaseContactDetailsTable.FieldContactId, ContactId, BaseContactDetailsTable.FieldReceiverId, receiverId);
            // 这里需要重新计算发送给了几个人，几个人已经阅读的功能
            this.SetReadState(ContactId);
            return returnValue;
        }

        /// <summary>
        /// 已阅读人数等计算
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>影响行数</returns>
        private int SetReadState(string id)
        {
            int returnValue = 0;
            // 发送人数
            string sqlQuery = " UPDATE Base_Contact "
                            + "    SET SendCount = ( "
                            + "                      SELECT COUNT(*) "
                            + "                        FROM Base_ContactDetails "
                            + "                       WHERE (Enabled = 1 AND ContactId = '" + id + "')) "
                            + "  WHERE Id = '" + id + "' ";
            returnValue += DbHelper.ExecuteNonQuery(sqlQuery);

            // 已阅读人数
            sqlQuery = " UPDATE Base_Contact "
                     + "    SET ReadCount = ( "
                     + "                      SELECT COUNT(IsNew) "
                     + "                        FROM Base_ContactDetails "
                     + "                       WHERE (Enabled = 1 AND ContactId = '" + id + "') AND (IsNew = 0)) "
                     + "  WHERE Id = '" + id + "'";
            returnValue += DbHelper.ExecuteNonQuery(sqlQuery);

            return returnValue;
        }

        #region public DataTable Read(string id) 阅读短信
        /// <summary>
        /// 阅读短信
        /// </summary>
        /// <param name="id">短信ID</param>
        /// <returns>数据集</returns>
        public DataTable Read(string id)
        {
            // 阅读处理
            DataTable dataTable = this.Get(id);
            BaseContactDetailsEntity contactDetailsEntity = new BaseContactDetailsEntity(dataTable);
            this.OnRead(contactDetailsEntity);
            // 返回结果
            BaseContactManager contactManager = new BaseContactManager(DbHelper, UserInfo);
            return contactManager.Get(contactDetailsEntity.ContactId);
        }
        #endregion

        #region private int OnRead(BaseContactDetailsEntity contactDetailsEntity) 阅读短信后设置状态值和阅读次数
        /// <summary>
        /// 阅读短信后设置状态值和阅读次数
        /// </summary>
        /// <param name="contactDetailsEntity">实体</param>
        /// <returns>影响的条数</returns>
        private int OnRead(BaseContactDetailsEntity contactDetailsEntity)
        {
            int returnValue = 0;
            // 设置邮件发送记录为已读状态
            // 标记为不是新邮件
            contactDetailsEntity.IsNew = 0;
            // 标记为无新评论
            contactDetailsEntity.NewComment = 0;
            // 最后访问日期
            contactDetailsEntity.LastViewDate = DateTime.Now.ToString();
            // 最后访问地址
            contactDetailsEntity.LastViewIP = UserInfo.IPAddress;
            // 更新实体
            returnValue += this.Update(contactDetailsEntity);
            // 阅读新邮件人数加
            this.SetReadState(contactDetailsEntity.ContactId);
            return returnValue;
        }
        #endregion

        /// <summary>
        /// 邮件有评论时要进行的操作
        /// </summary>
        /// <param name="detailsID">邮件主键</param>
        /// <returns>影响行数</returns>
        public int OnCommnet(string detailsId)
        {
            int returnValue = 0;
            BaseContactDetailsEntity contactDetailsEntity = this.GetEntity(detailsId);            
            // 更新子表的其他人的通知，首先是有效的邮件，其次不是新邮件，其次不是自己的邮件
            string sqlQuery = " UPDATE Base_ContactDetails "
                            + "    SET NewComment = 1 "
                            + "  WHERE (Enabled = 1) "
                            + "        AND (IsNew = 0) "
                            + "        AND (ContactId = '" + contactDetailsEntity.ContactId + "') "
                            + "        AND (ID <> '" + detailsId + "')";
            returnValue += DbHelper.ExecuteNonQuery(sqlQuery);
            BaseContactManager contactManager = new BaseContactManager(DbHelper, UserInfo);
            BaseContactEntity contactEntity = contactManager.GetEntity(contactDetailsEntity.ContactId);
            // 更新主表状态，评论人、评论时间发布上去
            contactEntity.CommentDate = DateTime.Now;
            contactEntity.CommentUserId = UserInfo.Id;
            contactEntity.CommentUserRealName = UserInfo.Realname;
            returnValue += contactManager.Update(contactEntity);
            return returnValue;
        }
    }
}
